package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSProperties;
import com.itheima.util.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传的Controller
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSProperties ossProperties;

    /**
     * 阿里云OSS
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传 , 上传的文件名: {}", file.getOriginalFilename());
        String extName = file.getOriginalFilename()
                .substring(file.getOriginalFilename()
                                   .lastIndexOf("."));
        String url = AliyunOSSUtils.upload(ossProperties.getEndpoint(), ossProperties.getBucketName(), file.getBytes(),
                                           extName);
        return Result.success(url);
    }

}
